package com.mall.order.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.mall.common.exception.ServiceException;
import com.mall.common.utils.DateUtils;
import com.mall.common.utils.SecurityUtils;
import com.mall.order.enums.OrderStatusEnum;
import com.mall.order.mapper.OrderMapper;
import com.mall.order.model.dto.OrderDto;
import com.mall.order.model.vo.OrderVo;
import com.mall.order.service.IOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 订单Service业务层处理
 *
 * @author mall
 * @date 2024-01-06
 */
@Service
public class OrderServiceImpl implements IOrderService
{
    @Resource
    private OrderMapper orderMapper;

    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public OrderVo selectOrderById(Long id)
    {
        return orderMapper.selectOrderById(id);
    }

    /**
     * 查询订单列表
     *
     * @param orderDto 订单
     * @return 订单
     */
    @Override
    public List<OrderVo> selectOrderList(OrderDto orderDto)
    {
        return orderMapper.selectOrderList(orderDto);
    }

    /**
     * 新增订单
     *
     * @param orderDto 订单
     * @return 结果
     */
    @Override
    public int insertOrder(OrderDto orderDto)
    {
        orderDto.setCreateTime(DateUtils.getNowDate());
        return orderMapper.insertOrder(orderDto);
    }

    /**
     * 修改订单
     *
     * @param orderDto 订单
     * @return 结果
     */
    @Override
    public int updateOrder(OrderDto orderDto)
    {
        orderDto.setUpdateTime(DateUtils.getNowDate());
        return orderMapper.updateOrder(orderDto);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderByIds(Long[] ids)
    {
        return orderMapper.deleteOrderByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderById(Long id)
    {
        return orderMapper.deleteOrderById(id);
    }

    @Override
    public int receiptOrder(OrderDto orderDto) {
        OrderVo orderVo = orderMapper.selectOrderById(orderDto.getId());
        if (null == orderVo) {
            throw new ServiceException("订单不存在");
        }
        orderVo.setConfirmReceiptFile(orderDto.getConfirmReceiptFile());
        orderVo.setConfirmReceiptRemark(orderDto.getConfirmReceiptRemark());

        OrderDto newOrderDto = new OrderDto();
        BeanUtil.copyProperties(orderVo, newOrderDto);
        newOrderDto.setOrderStatus(OrderStatusEnum.YET_COMPLETE.value());
        newOrderDto.setUpdateBy(SecurityUtils.getNickName());
        newOrderDto.setUpdateTime(DateUtils.getNowDate());
        // 更新订单
        return orderMapper.updateOrder(newOrderDto);
    }

    @Override
    public int refundOrder(OrderDto orderDto) {
        OrderVo orderVo = orderMapper.selectOrderById(orderDto.getId());
        if (null == orderVo) {
            throw new ServiceException("订单不存在");
        }
        orderVo.setConfirmReceiptFile(orderDto.getConfirmReceiptFile());
        orderVo.setConfirmReceiptRemark(orderDto.getConfirmReceiptRemark());

        OrderDto newOrderDto = new OrderDto();
        BeanUtil.copyProperties(orderVo, newOrderDto);
        newOrderDto.setOrderStatus(OrderStatusEnum.YET_REFUND.value());
        newOrderDto.setUpdateBy(SecurityUtils.getNickName());
        newOrderDto.setUpdateTime(DateUtils.getNowDate());
        // 更新订单
        return orderMapper.updateOrder(newOrderDto);
    }

    /**
     * 查询订单列表
     *
     * @param orderDto 订单
     * @return 订单
     */
    @Override
    public List<OrderVo> selectSignupOrderList(OrderDto orderDto)
    {
        return orderMapper.selectSignupOrderList(orderDto);
    }
}
